package com.daxia.bumall.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.bumall.dao.DecorateRequirementDAO;
import com.daxia.bumall.dto.DecorateRequirementDTO;
import com.daxia.bumall.model.DecorateRequirement;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.util.BeanMapper;

@Service
public class DecorateRequirementService {
	
	@Autowired
	private DecorateRequirementDAO decorateRequirementDAO;
	
	public List<DecorateRequirementDTO> find(DecorateRequirementDTO query, Page page) {
		List<DecorateRequirement> models = decorateRequirementDAO.find(query, page);
		List<DecorateRequirementDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<DecorateRequirementDTO> findAll() {
		return this.find(new DecorateRequirementDTO(), null);
	}
	
	public List<DecorateRequirementDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new DecorateRequirementDTO(), page);
	}
	
	public Long create(DecorateRequirementDTO dto) {
		DecorateRequirement model = new DecorateRequirement();
		toModel(model, dto);
		return decorateRequirementDAO.create(model);
	}
	
	public DecorateRequirementDTO load(Long id) {
	    DecorateRequirement model = decorateRequirementDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(DecorateRequirementDTO dto) {
		DecorateRequirement model = decorateRequirementDAO.load(dto.getId());
		toModel(model, dto);
		decorateRequirementDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				decorateRequirementDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public DecorateRequirementDTO findOne(DecorateRequirementDTO query) {
		DecorateRequirement model = decorateRequirementDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<DecorateRequirementDTO> toDTOs(List<DecorateRequirement> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<DecorateRequirementDTO>(0);
		}
		List<DecorateRequirementDTO> dtos = new ArrayList<DecorateRequirementDTO>(models.size());
		for (DecorateRequirement model : models) {
	        DecorateRequirementDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private DecorateRequirementDTO toDTO(DecorateRequirement model) {
		if (model == null) {
			return null;
		}
		DecorateRequirementDTO dto = BeanMapper.map(model, DecorateRequirementDTO.class);
		
		return dto;
	}
	
	private void toModel(DecorateRequirement model, DecorateRequirementDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<DecorateRequirement> toModels(List<DecorateRequirementDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<DecorateRequirement>(0);
		}
		List<DecorateRequirement> models = new ArrayList<DecorateRequirement>(dtos.size());
		for (DecorateRequirementDTO dto : dtos) {
	        DecorateRequirement model = new DecorateRequirement();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
	
	public Integer updateByIdAndDesignerId(Long id, Long designerId) {
		return decorateRequirementDAO.updateByIdAndDesignerId(id, designerId);
	}
	
	public BigInteger countAllOfADay() {
		return decorateRequirementDAO.countAllOfADay();
	}
	
	public BigInteger countSuccedOfADay() {
		return decorateRequirementDAO.countSuccedOfADay();
	}
}
